home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.05 May 96 / Memory Madness / MemoryMadnessC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  2.6 KB  |  135 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2.  
  3. #include "MemoryMadnessC.h"
  4.  
  5. static void dont_ever_mess_with_the_master_pointer (Handle h)  // DONT DO THIS!
  6. /* Memory Madness point 3 */
  7. {
  8.     int i;
  9.      
  10.     h = NewHandle( 10 );
  11.     for ( i = 0; i < 10; i++ ) {
  12.         *(*h)++ = 0;
  13.     }
  14. }
  15.  
  16. static void dont_know_what_kind_of_handle_we_have ()  // DONT DO THIS!
  17. /* Memory Madness point 10 */
  18. {
  19.     StringHandle h;
  20.     
  21.     h = (StringHandle) GetResource( 'STR ', 128 );
  22.     if ( h == NULL ) {
  23.         h = NewString( "\pHello" );
  24.     }
  25.     // Is h a resource or memory handle
  26. }
  27.  
  28. static void know_what_kind_of_handle_we_have ()
  29. /* Memory Madness point 10 */
  30. {
  31.     StringHandle h;
  32.     
  33.     h =  (StringHandle) GetResource( 'STR ', 128 );
  34.     if ( h == NULL ) {
  35.         h = NewString( "\pHello" );
  36.     } else {
  37.         DetachResource( (Handle) h );
  38.     }
  39.     // use h
  40.     DisposeHandle( (Handle) h );
  41. }
  42.  
  43. static void convert_between_memory_and_resource_handles_as_appropriate () 
  44. /* Memory Madness point 11 */
  45. {
  46.     int err;
  47.     Handle h;
  48.     
  49.     h =  Get1Resource( 'STR ', 128 );
  50.     err = ResError();
  51.     if ( h != NULL ) {
  52.         DetachResource( h );
  53.         AddResource(  h, 'STR ', 129 , "\pteststring");
  54.         err = ResError();
  55.     }
  56.     // h is NULL or will be disposed when the resource file is closed
  57. }
  58.  
  59.  
  60. static void printstring(Handle h)
  61. {
  62.     SInt8 state;
  63.     
  64.     state = HGetState(h);
  65.     HLock(h);
  66.     p2cstr((StringPtr) *h);
  67.     printf("%s\n",*h);
  68.     c2pstr(*h);
  69.     HSetState(h, state);
  70. }
  71.  
  72. static void demonstratemunger(void) 
  73. /* Memory Madness point 13*/
  74. {
  75.     OSErr err;
  76.     long where;
  77.     Handle h;
  78.     
  79.     printf("\n");
  80.     h = (Handle) NewString( "\pHello World!" );
  81.     
  82.     printstring(h);
  83.     
  84.     (void) Munger( h, 7, nil, 0, (Ptr) "Cruel ", 6 ); // insert "Cruel "
  85.     err = MemError();
  86.     (**h) = GetHandleSize( h ) - 1; // Correct Pascal String Length;
  87.  
  88.     printstring(h);
  89.     
  90.     DisposeHandle(h);
  91.     
  92.     printf("\n");
  93.     h = (Handle) NewString( "\pHello Cruel World!" );
  94.     (**h) = GetHandleSize( h ) - 1;
  95.     
  96.     printstring(h);
  97.  
  98.     (void) Munger( h, 6, nil, 6, &h, 0 ); // delete "Cruel "
  99.     (**h) = GetHandleSize( h ) - 1;
  100.     
  101.     printstring(h);
  102.  
  103.     DisposeHandle(h);
  104.  
  105.     printf("\n");
  106.     h = (Handle) NewString( "\pHello Cruel World!      " );
  107.  
  108.     printstring(h);
  109.  
  110.     where = Munger( h, 0, (Ptr) "Cruel ", 6, (Ptr) "Wonderful ", 10 ); // replace "Cruel " with "Wonderful "
  111.     err = MemError();
  112.     (**h) = GetHandleSize( h ) - 1;
  113.     
  114.     printstring(h);
  115.  
  116.     DisposeHandle(h);
  117.     
  118.     printf("\n");
  119.     h = (Handle) NewString( "\pHello Cruel World!" );
  120.     
  121.     printstring(h);
  122.  
  123.     where = Munger( h, 0, (Ptr) "Cruel ", 6, nil, 0 ); // find "Cruel "
  124.     if ( where >= 0 ) {
  125.         printf( "Found Cruel at offset %ld (remember, offsets are zero based, but pascal strings start with a length)\n", where );
  126.     }
  127.     DisposeHandle(h);
  128.  
  129. }
  130.  
  131. pascal void DoCStuff()
  132. {
  133.     demonstratemunger();
  134. }
  135.